#import "NotificationRequest.h"
#import "RequestConfig.h"

@interface NotificationRequest(){}
@property (nonatomic,assign)NSString *host;
@property (nonatomic,retain)AFHTTPSessionManager *manager;
@end

NotificationRequest *default_notification_request;
// content
@implementation NotificationRequest
- (void)initReq:(RequestConfig*)config{
    _host = [config host];
    _manager = [config manager];
}
+ (id)defaultNotificationRequest{
    if (default_notification_request == nil) {
        default_notification_request = [[NotificationRequest alloc]init];
        [default_notification_request initReq:[RequestConfig defaultConfig]];
    }
    return default_notification_request;
}
-(void)getAllNotification:(void (^)(NSArray<Notification *> *))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/notification/all",_host];
    [_manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            NSMutableArray<Notification*>* list = [[NSMutableArray alloc]init];
            NSArray *msgs = responseObject[@"Notification"];
            for (NSDictionary* msg in msgs) {
                Notification *n = [[Notification alloc]initWith:msg];
                [list addObject:n];
            }
            success(list);
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)getUnreadNotificationNum:(NSString *)NotificationID success:(void (^)(NSInteger))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/notification/unread",_host];
    [_manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            NSInteger num = (NSInteger)responseObject[@"Data"];
            success(num);
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)deleteNotificationByID:(NSString *)NotificationID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/notification/%@",_host,NotificationID];
    [_manager DELETE:url parameters:nil headers:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)readNotificationByID:(NSString *)NotificationID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/notification/read/%@",_host,NotificationID];
    NSDictionary *dict = @{@"isRead":@YES};
    [_manager PATCH:url parameters:dict headers:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)unreadNotificationByID:(NSString *)NotificationID success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/notification/read/%@",_host,NotificationID];
    NSDictionary *dict = @{@"isRead":@NO};
    [_manager PATCH:url parameters:dict headers:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}

@end
